﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace Mozlite.Cryptography
{
    internal class EncryptTransformer
    {
        private EncryptionAlgorithm algorithm;
        private byte[] iv;
        private byte[] key;

        internal EncryptTransformer(EncryptionAlgorithm algorithm)
        {
            this.algorithm = algorithm;
        }

        internal ICryptoTransform GetCryptoServiceProvider(byte[] privateKey)
        {
            // 选取提供程序。
            switch (algorithm)
            {
                case EncryptionAlgorithm.Des:
                    {
                        DES des = new DESCryptoServiceProvider();
                        des.Mode = CipherMode.CBC;

                        // 查看是否提供了密钥
                        if (null == privateKey)
                        {
                            key = des.Key;
                        }
                        else
                        {
                            des.Key = privateKey;
                            key = des.Key;
                        }
                        // 查看客户端是否提供了初始化向量
                        if (null == iv)
                        { // 让算法创建一个
                            iv = des.IV;
                        }
                        else
                        { //不，将它提供给算法
                            des.IV = iv;
                        }
                        return des.CreateEncryptor();
                    }
                case EncryptionAlgorithm.TripleDes:
                    {
                        TripleDES des3 = new TripleDESCryptoServiceProvider();
                        des3.Mode = CipherMode.CBC;
                        // See if a key was provided
                        if (null == privateKey)
                        {
                            key = des3.Key;
                        }
                        else
                        {
                            des3.Key = privateKey;
                            key = des3.Key;
                        }
                        // 查看客户端是否提供了初始化向量
                        if (null == iv)
                        { //是，让算法创建一个
                            iv = des3.IV;
                        }
                        else
                        { //不，将它提供给算法。
                            des3.IV = iv;
                        }
                        return des3.CreateEncryptor();
                    }
                case EncryptionAlgorithm.Rc2:
                    {
                        RC2 rc2 = new RC2CryptoServiceProvider();
                        rc2.Mode = CipherMode.CBC;
                        // 测试是否提供了密钥
                        if (null == privateKey)
                        {
                            key = rc2.Key;
                        }
                        else
                        {
                            rc2.Key = privateKey;
                            key = rc2.Key;
                        }
                        // 查看客户端是否提供了初始化向量
                        if (null == iv)
                        { //是，让算法创建一个
                            iv = rc2.IV;
                        }
                        else
                        { //不，将它提供给算法。
                            rc2.IV = iv;
                        }
                        return rc2.CreateEncryptor();
                    }
                case EncryptionAlgorithm.Rijndael:
                    {
                        Rijndael rijndael = new RijndaelManaged();
                        rijndael.Mode = CipherMode.CBC;
                        // 测试是否提供了密钥
                        if (null == privateKey)
                        {
                            key = rijndael.Key;
                        }
                        else
                        {
                            rijndael.Key = privateKey;
                            key = rijndael.Key;
                        }
                        // 查看客户端是否提供了初始化向量
                        if (null == iv)
                        { //是，让算法创建一个
                            iv = rijndael.IV;
                        }
                        else
                        { //不，将它提供给算法。
                            rijndael.IV = iv;
                        }
                        return rijndael.CreateEncryptor();
                    }
                default:
                    {
                        throw new CryptographicException("算法 ID '" + algorithm + "' 不受支持。");
                    }
            }
        }

        internal byte[] IV
        {
            get { return iv; }
            set { iv = value; }
        }

        internal byte[] Key
        {
            get { return key; }
        }
    }
}
